package cn.wit.summit.chwan.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.CommentAllListAdapter;
import cn.wit.summit.chwan.models.dto.CommentBaseBean;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.NetWorkUtils;
import cn.wit.summit.chwan.widgets.HistogramView;
import cn.wit.summit.chwan.widgets.MStarBar;
import cn.wit.summit.chwan.widgets.refreshLayout.SmartRefreshLayout;

/**
 * Created by Alan on 2018/4/22.
 */

public class CommentAllListActivity extends MyActivity implements CommentAllListAdapter.Listener {

    @Bind(R.id.treviBarTitleTx)
    TextView treviBarTitleTx;
    @Bind(R.id.treviBarRightTx)
    TextView treviBarRightTx;
    @Bind(R.id.treviRootBarRl)
    RelativeLayout treviRootBarRl;
    @Bind(R.id.commentAllList)
    ListView commentAllList;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private String commentId;
    private String commentContent;
    private float commentStars;
    private String gameId;
    private String packageName;
    private String commentScoreSwitch;//星星开关 1：开，0：关
    private int bespeakSwitch;//是否是预约游戏 1：开，0：关
    private int sgcSwitch;//游戏启动次数 1：开，0：关
    private String gameType;
    private int gameIsStart;

    private View headView;
    private LinearLayout start_ll;
    private LinearLayout point_ll;
    private LinearLayout goCommit;
    private MStarBar commentHeadMstarBar;
    private TextView editText5;
    private TextView comment_head_nopoint_tx;
    private TextView commentHeadPointTx;
    private TextView commentHeadNumTx;
    private HistogramView oneHv, twoHv, threeHv, fourHv, fiveHv;

    private CommentAllListAdapter mAdapter;
    private List<CommentAllListAdapter.ViewBean> dataSet = new ArrayList<>();
    private int uid;

    private int pn = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_all_list);
        ButterKnife.bind(this);

        hideTitleBar();

        initData();
        initView();
        initListView();
    }

    private void initListView() {
        mAdapter = new CommentAllListAdapter(mContext, uid, dataSet, commentScoreSwitch, sgcSwitch, this);
        if (commentScoreSwitch!=null && commentScoreSwitch.equals("1")) {//星星开关开启
            commentAllList.addHeaderView(headView);
            treviBarRightTx.setVisibility(View.GONE);
        } else {
            treviBarRightTx.setVisibility(View.VISIBLE);
        }
        commentAllList.setAdapter(mAdapter);
    }

    private void initView() {
        headView = LayoutInflater.from(this).inflate(R.layout.comment_list_head_view, null);
        oneHv = (HistogramView) headView.findViewById(R.id.oneHv);//#F47500
        twoHv = (HistogramView) headView.findViewById(R.id.twoHv);//#F47500
        threeHv = (HistogramView) headView.findViewById(R.id.threeHv);//#F47500
        fourHv = (HistogramView) headView.findViewById(R.id.fourHv);//#F47500
        fiveHv = (HistogramView) headView.findViewById(R.id.fiveHv);//#F47500
        commentHeadMstarBar = (MStarBar) headView.findViewById(R.id.comment_head_mstarBar);
        commentHeadPointTx = (TextView) headView.findViewById(R.id.comment_head_point_tx);
        comment_head_nopoint_tx = (TextView) headView.findViewById(R.id.comment_head_nopoint_tx);
        commentHeadNumTx = (TextView) headView.findViewById(R.id.comment_head_num_tx);
        editText5 = (TextView) headView.findViewById(R.id.editText5);
        editText5.setText("玩过");
        start_ll = (LinearLayout) headView.findViewById(R.id.start_ll);
        point_ll = (LinearLayout) headView.findViewById(R.id.point_ll);
        headView.findViewById(R.id.goCommit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textTopRight();
            }
        });
        commentHeadMstarBar.setIntegerMark(false);
        commentHeadMstarBar.setEnabled(false);

        treviBarRightTx.setText(getResources().getString(R.string.comment_all_right_str));
        treviBarTitleTx.setText(getResources().getString(R.string.comment_all_title_str));
    }

    private void initData() {
        Intent intent = getIntent();
        commentId = intent.getStringExtra("commentId");
        commentContent = intent.getStringExtra("commentContent");
        gameId = intent.getStringExtra("gameId");
        gameType = intent.getStringExtra("gameType");
        packageName = intent.getStringExtra("packageName");
        commentScoreSwitch = intent.getStringExtra("commentScoreSwitch");
        commentStars = intent.getFloatExtra("commentStars", 0f);
        gameIsStart = intent.getIntExtra("gameIsStart", 0);
        bespeakSwitch = intent.getIntExtra("bespeakSwitch", 0);
        sgcSwitch = intent.getIntExtra("sgcSwitch", 0);
    }

    private void showMain(List<CommentBaseBean> commentBaseBeans) {
        if (pn == 1) {
//            if (commentBaseBeans.size()>0) {
//                comment_all_list.setVisibility(View.VISIBLE);
//                mPtrFrame.setVisibility(View.VISIBLE);
//                noCommentLl.setVisibility(View.GONE);
//            } else {
//                comment_all_list.setVisibility(View.GONE);
//                mPtrFrame.setVisibility(View.GONE);
//                noCommentLl.setVisibility(View.VISIBLE);
//            }
//            loding_faile.setVisibility(View.GONE);
//            loding_layout.setVisibility(View.GONE);
//            commentBaseBeanList.clear();
            dataSet.clear();
        }
        pn++;
        addViewItem(commentBaseBeans);
        mAdapter.notifyDataSetChanged();
    }

    private void addViewItem(List<CommentBaseBean> list) {

        for (int i = 0; null != list && i < list.size(); i++) {
            CommentBaseBean bean = list.get(i);
            if (bean.isTitle()) {
                CommentAllListAdapter.ViewBean viewBean = new CommentAllListAdapter.ViewBean(CommentAllListAdapter.ViewType.TITLE, new CommentAllListAdapter.ViewBean.Comment(bean));
                dataSet.add(viewBean);
            } else {
                CommentAllListAdapter.ViewBean viewBean = new CommentAllListAdapter.ViewBean(CommentAllListAdapter.ViewType.COMMENT, new CommentAllListAdapter.ViewBean.Comment(bean));
                dataSet.add(viewBean);
            }
        }

    }

    @OnClick({R.id.treviBarBackIv, R.id.treviBarRightTx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.treviBarBackIv:
                finish();
                break;
            case R.id.treviBarRightTx://发表点评
                textTopRight();
                break;
        }
    }

    private void textTopRight() {
        //是否登陆
        if (AccountUtil.getInstance().getUid()==0) {
            IntentUtil.getInstance().goLoginActivity(mContext);
            return;
        }

        Intent intent = new Intent(mContext, CommentCreatActivity.class);
        intent.putExtra("gameId", gameId);
        intent.putExtra("packageName", packageName);
        intent.putExtra("commentScoreSwitch", commentScoreSwitch);
        intent.putExtra("bespeakSwitch", bespeakSwitch);
        intent.putExtra("gameType", gameType);
        intent.putExtra("gameIsStart", gameIsStart);

        //判断有没有自己的评论
//                if (mAdapter.getMyCommentPosition()==-1 || mySelf==null) {
//                } else {
//                    intent.putExtra("commentId", );
//                    intent.putExtra("commentContent", );
//                    intent.putExtra("commentStars", );
//                }

        startActivity(intent);
    }

    @Override
    public void onPraise(CommentBaseBean bean, int position, int type) {
        if (NetWorkUtils.isNetworkConnected(mContext)) {
            try {
                // 这里设置请求参数
//                RequestCommentpraiseArgs commonRequestBean = getCommentPraiseRequestBean(bean.getId(), type, bean.getPn());
//                CommentResponse<CommentPraiseBean> commitBean = rpcClient.getCommentPraise(commonRequestBean);
//                if (null != commitBean) {
//                    if (commitBean.getCode()==801) {
//                        resultShow2(commitBean);
//                    } else if (commitBean.getSucc()!=1) {
//                        resultShow(commitBean.getMsg(), true, position, 1);
//                    }
//                    if (commitBean.getSucc()==1)
//                        checkReset(position, 1);
//                } else {
//                    resultShow("操作失败，请稍候再试~", true, position, 1);
//                }
            } catch (Exception e) {
                resultShow("操作失败，请稍候再试~", true, position, 1);
                e.printStackTrace();
            }
        } else {
            resultShow("网络连接失败，再试试吧~", true, position, 1);
        }
    }

    @Override
    public void onUnPraise(CommentBaseBean bean, int position, int type) {
        if (NetWorkUtils.isNetworkConnected(mContext)) {
//            try {
//                // 这里设置请求参数
//                RequestCommentpraiseArgs commonRequestBean = getCommentPraiseRequestBean(bean.getId(), type, bean.getPn());
//                CommentResponse<CommentPraiseBean> commitBean = rpcClient.getCommentDown(commonRequestBean);
//                if (null != commitBean) {
//                    if (commitBean.getCode()==801) {
//                        resultShow2(commitBean);
//                    } else if (commitBean.getSucc()!=1) {
//                        resultShow(commitBean.getMsg(), true, position, 2);
//                    }
//                    if (commitBean.getSucc()==1)
//                        checkReset(position, 2);
//                } else {
//                    resultShow("操作失败，请稍候再试~", true, position, 2);
//                }
//            } catch (Exception e) {
//                resultShow("操作失败，请稍候再试~", true, position, 2);
//                e.printStackTrace();
//            }
        } else {
            resultShow("网络连接失败，再试试吧~", true, position, 2);
        }
    }

    @Override
    public void onRootView(CommentBaseBean bean) {
//        CommentDetailActivity_.intent(context).gameId(bean.getGame_id()).commentId(bean.getId())
//                .gameType(gameType).bespeakSwitch(bespeakSwitch)
//                .gameIsStart(gameIsStart).sgcSwitch(sgcSwitch)
//                .packageName(packageName).commentScoreSwitch(commentScoreSwitch).start();
    }

    public void checkReset(int position, int i) {
        if (i==1) {
            mAdapter.resetDespise(position, 1);
        } else {
            mAdapter.resetPraise(position, 1);
        }
    }

    private void resultShow(String msg, boolean b, int position, int type) {
        showToast(msg);
        if (b) {
            if (type==1)
                mAdapter.resetPraise(position, 0);
            else
                mAdapter.resetDespise(position, 0);
        }
    }
}
